package app.ddknight.passwordgenerator;

import android.content.ClipData;
import android.content.ClipboardManager;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {
    private EditText et_password;
    private Button btn_refresh;
    private ClipboardManager cm;
    private ClipData cd;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        String appName = getResources().getString(R.string.app_name);
        String appAuther = getResources().getString(R.string.app_auther);
        MainActivity.this.setTitle(appName + "\tDeveloped By " + appAuther);
        cm = (ClipboardManager) getSystemService(CLIPBOARD_SERVICE);
        btn_refresh = (Button) MainActivity.this.findViewById(R.id.button);
        et_password = (EditText) MainActivity.this.findViewById(R.id.editText);
        btn_refresh.setOnClickListener(new btnListener());
    }

    class btnListener implements View.OnClickListener {
        @Override
        public void onClick(View view) {
            btn_refresh.setText(R.string.btn_refresh);
            String newPassword = PasswordGenerator.getPassword();
            et_password.setText(newPassword);
            cd = ClipData.newPlainText("text", newPassword);
            cm.setPrimaryClip(cd);
            String messageFormat = getResources().getString(R.string.msg_password_has_been_copyed);
            Toast.makeText(getApplicationContext(), String.format(messageFormat, newPassword), Toast.LENGTH_SHORT).show();
        }
    }


}
